<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 会员管理中心
 */
namespace app\system\controller\passport;
use app\common\facade\Alisms;
use app\common\event\Passport;
use app\common\model\Member;
use app\common\model\MemberMiniapp;

class Login extends Common{

    public function initialize(){
        parent::initialize();
    }
    
    /**
     * 会员首页
     */
    public function index(){
        if(request()->isPost()){
            $data = [
                '__token__'      => $this->request->param('__token__/s'),
                'captcha'        => $this->request->param('captcha/s'),
                'login_id'       => $this->request->param('login_id/s'),
                'login_password' => $this->request->param('login_password/s'),
            ];
            $validate = $this->validate($data,'Member.login');
            if(true !== $validate){
                return json(['code'=>0,'message'=>$validate]);
            }
            $result  = Member::login($data);
            if($result){
                Passport::clearMiniapp();
                Passport::setlogout();
                //判断是不是子管理员
                $condition['is_lock'] = 0;
                if($result->bind_member_miniapp_id && $result->parent_id){
                    $condition['id']        = $result->bind_member_miniapp_id;
                    $condition['member_id'] = $result->parent_id;
                }else{
                    $condition['member_id'] = $result->id;
                }
                $miniapp = MemberMiniapp::where($condition)->order('id desc')->find();
                if(empty($miniapp)){
                    return json(['code'=>0,'message'=>'应用已被锁定,员工账号禁止登陆']);
                }
                $param = [
                    'member_id'         => $result->parent_id,
                    'miniapp_id'        => $miniapp->miniapp_id,
                    'member_miniapp_id' => $miniapp->id,
                ];
                Passport::setMiniapp($param);
                Passport::setLogin($result);
                return json(['code'=>200,'message'=>'登录成功','url' => url('system/passport.Index/index')]);
            }else{
                return json(['code'=>0,'message'=>'密码或账户错误']);
            }
        }else{
            return view('passport/login/index');
        }
    }

    /**
     * 会员注册
     */
    public function reg(){
        if(request()->isPost()){
            $data = [
                'captcha'        => $this->request->param('captcha/s'),
                'phone_id'       => $this->request->param('phone_id/s'),
                'login_password' => $this->request->param('login_password/s'),
                'sms_code'       => $this->request->param('sms_code/s'),
                'username'       => $this->request->param('username/s'),
            ];
            $validate = $this->validate($data,'Member.reg');
            if(true !== $validate){
                return json(['code'=>0,'message'=>$validate]);
            }
            $result  = Member::reg($data);
            if($result['code'] == 200){
                $userinfo = Member::where(['id' => $result['data']['id']])->find();
                Passport::clearMiniapp();
                Passport::setlogout();
                Passport::setLogin($userinfo);
                return json(['code'=>200,'message'=>'注册成功','url' => url('system/passport.Index/index')]);
            }else{
                return json($result);
            }
        }else{
            return view('passport/login/reg');
        }
    }

    /**
     * 忘记密码
     */
    public function getPassword(){
        if(request()->isPost()){
            $data = [
                'captcha'        => $this->request->param('captcha/s'),
                'phone_id'       => $this->request->param('phone_id/s'),
                'login_password' => $this->request->param('login_password/s'),
                'sms_code'       => $this->request->param('sms_code/s'),
            ];
            $validate = $this->validate($data,'Member.getpasspord');
            if(true !== $validate){
                return json(['code'=>0,'message'=>$validate]);
            }
            //判断验证码
            $sms['phone_id'] = $data['phone_id'];
            $sms['sms_code'] = $data['sms_code'];
            $is_sms = widget('common/alisms/isSms',$sms);
            if(!$is_sms){
                return json(['code'=>0,'message'=>"验证码错误"]);
            }
            //验证码通过
            $result  = Member::getPasspord($data);
            if($result){
                Passport::clearMiniapp();
                Passport::setlogout();
                return json(['code'=>200,'message'=>'密码重置成功','url' => url('system/passport.Login/index')]);
            }else{
                return json(['code'=>0,'message'=>'密码重置失败']);
            }
        }else{
            return view('passport/login/getpassword');
        }
    }

    /**
     * 会员退出
     */
    public function logout(){
        Passport::setlogout();
        Passport::clearMiniapp();
        $this->redirect('system/passport.Login/index');
    }

    /**
     * 获取注册验证码
     */
    public function getRegSms(){
        if(request()->isPost()){
            $data = [
                'phone_id'  => $this->request->param('phone/s')
            ];
            $validate = $this->validate($data,'AliSms.getsms');
            if(true !== $validate){
                return json(['code'=>0,'message'=>$validate]);
            }
            $user  = Member::where(['phone_id' => $data['phone_id']])->find();
            if(isset($user)) {
                return json(['code'=>0,'message' => "手机已被注册"]);
            }
            $sms = Alisms::putSms($data['phone_id']);
            return json($sms);
        }else{
            return $this->error("404 NOT FOUND");
        }
    }

    /**
     * 获取登录/找回密码等验证码
     */
    public function getLoginSms(){
        if(request()->isPost()){
            $data = [
                'phone_id' => $this->request->param('phone/s')
            ];
            $validate = $this->validate($data,'AliSms.getsms');
            if(true !== $validate){
                return json(['code'=>0,'message'=>$validate]);
            }
            //判断是否登录
            $getuser = Passport::getUser();
            if($getuser){
                if($data['phone_id'] != $getuser['phone_id']){
                    return json(['code'=>0,'message'=>"请确认手机号输入正确"]);
                }
            }
            $user  = Member::where(['phone_id' => $data['phone_id']])->find();
            if(empty($user)) {
                return json(['code'=>0,'message'=>"用户不存在"]);
            }
            $sms = Alisms::putSms($data['phone_id']);
            return json($sms);
        }else{
            return $this->error("404 NOT FOUND");
        }
    }
}